<template>
	<view class="content mainpadding2">
		<u-tabs :list="tabList" :scrollable="false" @click="tabchange" :current="tabcurrent" lineWidth="60rpx"
			lineColor="rgba(255, 184, 0, 1)" :activeStyle="{
		  color: '#000000',
		  fontWeight: 800,
		  transform: 'scale(1.05)'}" :inactiveStyle="{
		  color: '#666666',
		    transform: 'scale(1)'
		}"></u-tabs>
		<template v-if="tabsid==1 || tabsid==2">
			<view class="mainpadding_top ffffff border2" v-for="item in list.data" :key="item.id"
				@click="jumpdetail('/pages/message/noticeDetail?id=',item.id)">
				<view class="flexbetween">
					<view class="stateBox text_e radius color2">{{item.title}}</view>
					<view class="color_six text_five margin_top2">{{item.createtime_text}}</view>
				</view>
				<view class="margin_top color_ling text_a padding_bottom border_bottom">
					{{item.des}}
				</view>
				<view class="flexright margin_top2">
					<view class="lanlable">看詳情</view>
				</view>
			</view>
		</template>
		<template v-if="tabsid==3">
			<view class="mainpadding_top radius shadow" style="background-color: #fbf6f1;" v-for="item in list.data"
				@click="jumpdetail('/pages/message/diaochaxq?id=',item.id)">
				<view class="flexbetween pdl" style="padding-bottom: 10rpx;">
					<view class="flexleft ">
						<view class="margin_right2">
							<image :src="item.image_text" mode="" class="radius"
								style="width: 120rpx;height: 120rpx;min-width: 120rpx;"></image>
						</view>
						<view class="line margin_right2"></view>
						<view>
							<view class="hei_text titletext bold" style="word-break: break-all;">{{item.name}}</view>
						</view>
					</view>
				</view>
				<view class="flexleft margin_top1">
					<view class="hui_text bold" style="font-size: 26rpx;">活動時間</view>
					<view class="hong_text bold " style="font-size: 26rpx;">
						{{item.dc_starttime_text}}~{{item.dc_endtime_text}}</view>
				</view>
			</view>
		</template>
		<template v-if="tabsid==4">
			<view class="mainpadding_top radius shadow dingwei" style="background-color: #fbf6f1;" v-for="item in list.data">
				<!-- 状态:1=待支付,2=待领取,3=已领取,4=已作废 -->
				<view class="checkfocus xiaobai" style="background-color: #ff0000;" v-show="item.status==1">待支付</view>
				<view class="checkfocus xiaobai" style="background-color: #ffb800;" v-show="item.status==2">待領取</view>
				<view class="checkfocus xiaobai" style="background-color: #06c763;" v-show="item.status==3">已領取</view>
				<view class="checkfocus xiaobai" style="background-color: #999999;" v-show="item.status==4">已作廢</view>
				<view class="flexbetween pdl" style="padding-bottom: 10rpx;">
					<view class="flexleft ">
						<view class="margin_right2">
							<image :src="item.image_text" mode="" class="radius"
								style="width: 120rpx;height: 120rpx;min-width: 120rpx;"></image>
						</view>
						<view class="line margin_right2"></view>
						<view>
							<view class="hei_text titletext bold" style="word-break: break-all;">{{item.name}}</view>
						</view>
					</view>
					<view class="use" @click="lingqu(item)" v-show="item.status==2 && item.get_starttime_text">領取</view>
				</view>
				<view class="flexleft margin_top1" v-if="item.get_starttime_text">
					<view class="hui_text bold" style="font-size: 26rpx;">領取時間</view>
					<view class="hong_text bold " style="font-size: 26rpx;">
						{{item.get_starttime_text}}~{{item.get_endtime_text}}</view>
				</view>
				<view class="margin_top1" v-if="item.status==2 && !item.get_starttime_text">
					<view class="hong_text bold " style="font-size: 26rpx;">等待領取時間，目前還不能領取哦~</view>
				</view>
				<view class="flexbetween margin_top1" v-if="item.pay_image">
					<view class="xiaohei main_size">支付憑證</view>
					<image class="listleftimg1 radius" :src="item.pay_image_text" mode=""></image>
				</view>
				<view class="flexright margin_top1" v-if="item.status==1">
					<view class="listbtnhuang"
						@click.stop="pzshow=true;currentid=item.id;">上傳憑證</view>
				</view>
			</view>
		</template>
		<u-modal :show="scanshow" title="掃碼領券" confirmText="關閉"
			@confirm="scanshow=false" @cancel="scanshow=false" confirmColor="#ffb800">
			<view class="slot-content" style="width: 600rpx;">
				<view class="flexcenter" v-if="scanshow">
					<uqrcode ref="uqrcode" canvas-id="qrcode" v-bind:value="qrcodeurl_text" :options="{ margin: 10}"
						size="300">
					</uqrcode>
				</view>
			</view>
		</u-modal>
		<!-- 上傳支付憑證 -->
		<u-modal :show="pzshow" title="上傳憑證" confirmColor="#ffb800" confirmText="確認" @confirm="upconfirm"
			cancelText="取消" @cancel="pzshow=false" showCancelButton>
			<view class="slot-content" style="width: 730rpx;">
				<view class="flexcenter">
					<view class="itemimgBox flexcenter margin_top2" v-for="(item,index) in images_text" :key="index">
						<!-- 已上傳 -->
						<view class="listleftimg1 radius">
							<image class="radius" :src="item" mode=""></image>
							<view class="deleteimg" @click="delindex(index,images,images_text)"></view>
						</view>
					</view>
					<view class="itemimgBox flexcenter margin_top2" @click="uploadImg(1,images,images_text)"
						v-if="images.length<1"><!-- 未上傳 -->
						<image class="listleftimg1 radius" src="@/static/image/system/upimg.png" mode=""></image>
					</view>
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				page: 1,
				limit: 12,
				list: {
					data: []
				},
				tabsid: 1,
				tabList: [{
					name: "公告",
					id: 1,
				}, {
					name: "活動",
					id: 2,
				}, {
					name: "調查",
					id: 3,
				}, {
					name: "領取",
					id: 4,
				}],
				tabcurrent: 0,
				scanshow:false,
				qrcodeurl_text: "",
				pzshow:false,
				currentid:"",
				images:[],
				images_text:[],
			}
		},
		onLoad(options) {
			this.tabsid = options.type
			this.changeToptitle()
			this.tabcurrent = Number(options.type) - 1
			this.init()
		},
		methods: {
			uploadImg(value, upImgs, upImgs_text) { //上傳圖片
				let _this = this;
				let num = Number(value) - upImgs.length;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相機', '相簿'],
					success: function(res) {
						// 選擇相機走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 選擇相簿走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			delindex(index, upImg, upImgs_text) {
				upImg.splice(index, 1)
				upImgs_text.splice(index, 1)
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				this.http.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
			upconfirm() {
				if (this.images.length == 0) {
					httpRequest.toast("請上傳圖片")
				}
				this.pzshow = false
				let url = "/api/Shopwelfarecl/updateOrderImage"
				let data = {
					id: this.currentid,
					pay_image: this.images.join()
				}
				httpRequest.request(url, 'GET', data).then(res => {
					httpRequest.toast(res.msg);
					if (res.code == 1) {
						this.images = []
						this.images_text = []
						this.init()
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			lingqu(item){
				this.qrcodeurl_text = "/pages/workbench/huodonghx?id="+item.id
				this.scanshow = true
			},
			tabchange(e) {
				this.tabsid = e.id
				this.changeToptitle()
				this.init()
				this.list.data = []
			},
			changeToptitle() {
				if (this.tabsid == 1) {
					uni.setNavigationBarTitle({
						title: "公告"
					})
				} else if (this.tabsid == 2) {
					uni.setNavigationBarTitle({
						title: "活動"
					})
				} else if (this.tabsid == 3) {
					uni.setNavigationBarTitle({
						title: "調查"
					})
				}
			},
			init(isPage, page) {
				let _this = this
				let url = "/api/index/ptnewsIndex"
				let data = {
					page: page || 1,
					limit: _this.limit,
					type: this.tabsid
				}
				if(this.tabsid==1 || this.tabsid==2) return
				if (this.tabsid == 3) {
					url = "/api/Shopwelfarecl/getWelfareIndex"
					data = {
						page: page || 1,
						limit: _this.limit,
						shop_id: uni.getStorageSync("shop_id")
					}
				}
				if (this.tabsid == 4) {
					url = "/api/Shopwelfarecl/welfareclOrder"
					data = {
						page: page || 1,
						limit: _this.limit,
						shop_id: uni.getStorageSync("shop_id")
					}
				}
				httpRequest.request(url, 'GET', data).then(res => {
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							this.list = res.data
							_this.list.data = resList.data;
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.stateBox {
		padding: 5rpx 15rpx;
	}

	.color2 {
		color: #fff;
		background: #9064ff;
	}
	.listleftimg1 {
		width: 150rpx;
		min-width: 150rpx;
		height: 150rpx;
		position: relative;
	
		image {
			width: 100%;
			height: 100%;
		}
	}
	.use {
		width: 90rpx;
		height: 49rpx;
		text-align: center;
		line-height: 49rpx;
		background: #ed4d2d;
		border-radius: 4rpx 4rpx 4rpx 4rpx;
		border: 1rpx solid #ed4d2d;
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #fff;
	}
	::v-deep .u-transition {
		z-index: 900 !important;
	}
</style>